package com.mikey.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mikey.common.PageResult;
import com.mikey.group.Specification;
import com.mikey.mapper.TbSpecificationMapper;
import com.mikey.mapper.TbSpecificationOptionMapper;
import com.mikey.pojo.TbSpecification;
import com.mikey.pojo.TbSpecificationExample;
import com.mikey.pojo.TbSpecificationOption;
import com.mikey.pojo.TbSpecificationOptionExample;
import com.mikey.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper specificationMapper;
    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    /**
     * 查询全部
     */
    @Override
    public List<TbSpecification> findAll() {
        return specificationMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<TbSpecification> page = (Page<TbSpecification>) specificationMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Specification specification) {
        //先把规格插入到表中
        specificationMapper.insert(specification.getSpecification());

        //循环插入规格选项
        for (TbSpecificationOption specificationOption : specification.getSpecificationOptionList()) {

            //设置规格ID
            specificationOption.setSpecId(specification.getSpecification().getId());

            specificationOptionMapper.insert(specificationOption);
        }
    }


    /**
     * 修改
     */
    @Override
    public void update(Specification specification) {
        //保存规格
        specificationMapper.updateByPrimaryKey(specification.getSpecification());
        //删除原有规格选项
        TbSpecificationOptionExample example=new TbSpecificationOptionExample();

        TbSpecificationOptionExample.Criteria criteria =example.createCriteria();

        //指定规格ID为条件
        criteria.andSpecIdEqualTo(specification.getSpecification().getId());
        //删除
        specificationOptionMapper.deleteByExample(example);

        //循环插入规格选项
        for (TbSpecificationOption specificationOption:specification.getSpecificationOptionList()){
            //关联规格Id
            specificationOption.setSpecId(specification.getSpecification().getId());
            //插入数据库
            specificationOptionMapper.insert(specificationOption);
        }
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Specification findOne(Long id) {
        TbSpecification tbSpecification=specificationMapper.selectByPrimaryKey(id);

        //查询规格选项列表
        TbSpecificationOptionExample example =new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria=example.createCriteria();
        //根据规格ID查询
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> optionList= specificationOptionMapper.selectByExample(example);
        //构建组合实体类返回结果
        Specification spec=new Specification();
        spec.setSpecification(tbSpecification);
        spec.setSpecificationOptionList(optionList);
        return  spec;
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            specificationMapper.deleteByPrimaryKey(id);
        }
    }


    @Override
    public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();

        if (specification != null) {
            if (specification.getSpecName() != null && specification.getSpecName().length() > 0) {
                criteria.andSpecNameLike("%" + specification.getSpecName() + "%");
            }
        }

        Page<TbSpecification> page = (Page<TbSpecification>) specificationMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Map> selectOptionList() {
        return specificationMapper.selectOptionList();
    }

}
